package com.tianxinweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.tianxinweb.entity.Information;
import com.tianxinweb.entity.Result;
import com.tianxinweb.enums.ResultEnum;
import com.tianxinweb.exception.MyException;
import com.tianxinweb.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cboy
 * @since 2018-08-07
 */
@RestController
@RequestMapping("/info")
public class InformationController {

    @Autowired
    private InformationService service;

    /**
     * 获取所有公告
     * @return
     */
    @RequestMapping("getAllInfo")
    public Result getAllInfo(){
        List<Information> list = service.selectList(null);
        return Result.success().add("list",list);
    }

    /**
     * 通过公告id获取公告详情
     * @param id
     * @return
     */
    @RequestMapping("getInfoById/{id}")
    public Result getInfoById(@PathVariable("id") Integer id){
        Information information = service.selectById(id);
        return Result.success().add("detail",information);
    }

    /**
     * 获取/搜索资讯列表
     * @param pn
     * @param size
     * @param key
     * @return
     */
    @RequestMapping("list")
    public Result list(@RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="size",defaultValue="10") Integer size,
                       @RequestParam(value = "key",defaultValue = "") String key) {
        Page page = service.getInfoList(pn,size,key);
        return Result.success().add("page", page);
    }


    /**
     * 获取推荐资讯
     * @return
     */
    @RequestMapping("getRecommendInfo")
    public Result getRecommendInfo(){
        List<Information> list = service.getRecommendInfo();
        return Result.success().add("list", list);
    }

    /**
     * 删除资讯
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id ){
        service.deleteById(id);
        return Result.success();
    }

    /**
     * 推荐/取消推荐资讯
     * @param id
     * @return
     */
    @RequestMapping("recommend/{id}")
    public Result recommend(@PathVariable("id") Integer id,@RequestParam(value="type",defaultValue = "1") Integer type){
        service.recommend(id,type);
        return Result.success();
    }

    /**
     * 阅读资讯记录
     * @return
     */
    @RequestMapping("reading/{id}")
    public Result reading(@PathVariable("id") Integer id){
        service.addReadingAmount(id);
        return Result.success();
    }

    /**
     * 添加资讯
     * @param info
     * @param result
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody @Valid Information info, BindingResult result){

        if(result.hasErrors()){
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        service.addInfo(info);
        return Result.success();
    }

    /**
     * 更新资讯
     * @param info
     * @param result
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody @Valid Information info, BindingResult result){

        if(result.hasErrors() || info.getId()==null){
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        service.updateInfo(info);
        return Result.success();
    }

}

